
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';


class MemberAnalysisScreen extends StatefulWidget {
  @override
  _MemberAnalysisScreenState createState() => _MemberAnalysisScreenState();
}

class _MemberAnalysisScreenState extends State<MemberAnalysisScreen> {


  var _pageController = new PageController(initialPage: 0);

  @override
  Widget build(BuildContext context) {

    List questTilts = [
      {'No':'A.','title':'小区的随机接入前导格式'},
      {'No':'B.','title':'小区半径'},
      {'No':'C.','title':'小区环境'},
      {'No':'D.','title':'小区宽带的配置'},
    ];
    List<QuestionModel> page1ModelList = [];
    List<QuestionModel> page2ModelList = [];
    List<QuestionModel> page3ModelList = [];
    List<QuestionModel> page4ModelList = [];
    List<QuestionModel> page5ModelList = [];
    for (var map in questTilts) {
      page1ModelList.add(QuestionModel.fromJson(map));
      page2ModelList.add(QuestionModel.fromJson(map));
      page3ModelList.add(QuestionModel.fromJson(map));
      page4ModelList.add(QuestionModel.fromJson(map));
      page5ModelList.add(QuestionModel.fromJson(map));
    }


    List<PageModel> pageModelList = [
      PageModel('第一页标题',true,page1ModelList),
      PageModel('第二页标题',false,page2ModelList),
      PageModel('第三页标题',true,page3ModelList),
      PageModel('第四页标题',false,page4ModelList),
      PageModel('第五页标题',true,page5ModelList),
    ];

    return Scaffold(
      appBar: AppBar(
        title: Text('会员分析'),
      ),
      body: PageView.builder(
          controller: _pageController,
          itemCount: pageModelList.length,
          itemBuilder: (BuildContext context,int index){
            return pageItem(pageModelList[index]);
          },
        ),
    );
  }

    Widget pageItem(PageModel pageModel){
    
    return Container(
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              Text(pageModel.isMultiSelect ? '多选题' : '单选题',style: TextStyle(backgroundColor: Colors.lightBlue,fontSize: 20),),
              Text(pageModel.title,style: TextStyle(fontSize: 20),)
            ],
          ),
          ListView.builder(
            itemCount: pageModel.questionList.length,
            shrinkWrap: true,
            itemBuilder: (BuildContext context,int index){
              QuestionModel questionModel = pageModel.questionList[index];
              return InkWell(
                onTap: (){
                  setState(() {
                    print('点击$index');
                    if (pageModel.isMultiSelect) {
                      questionModel.isSelected = !questionModel.isSelected;
                    }else{
                      for (QuestionModel item in pageModel.questionList) {
                        item.isSelected = false;
                      }
                      questionModel.isSelected = true;
                    }
                  });
                },
                child: Container(
                  child: Row(
                    children: <Widget>[
                      SizedBox(width: 10),
                      Container(
                        padding: EdgeInsets.all(3),
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          border: Border.all(color: questionModel.isSelected ? Colors.red : Colors.green,width: 2),
                        ),
                        child: Center(
                          child: Text(questionModel.number,style: TextStyle(fontSize: 20)),
                        ),
                      ),
                      Text(questionModel.questionTitle,style: TextStyle(fontSize: 20))
                    ],
                  )
                ),
              );
            },
          )
        ],
      ),
    );

  }
}

// class MemberAnalysisScreen extends StatelessWidget {

//   //int _currentPageIndex = 0;
//   var _pageController = new PageController(initialPage: 0);

//   @override
//   Widget build(BuildContext context) {

//     List questTilts = [
//       {'No':'A.','title':'小区的随机接入前导格式'},
//       {'No':'B.','title':'小区半径'},
//       {'No':'C.','title':'小区环境'},
//       {'No':'D.','title':'小区宽带的配置'},
//     ];
//     List<QuestionModel> page1ModelList = [];
//     List<QuestionModel> page2ModelList = [];
//     List<QuestionModel> page3ModelList = [];
//     List<QuestionModel> page4ModelList = [];
//     List<QuestionModel> page5ModelList = [];
//     for (var map in questTilts) {
//       page1ModelList.add(QuestionModel.fromJson(map));
//       page2ModelList.add(QuestionModel.fromJson(map));
//       page3ModelList.add(QuestionModel.fromJson(map));
//       page4ModelList.add(QuestionModel.fromJson(map));
//       page5ModelList.add(QuestionModel.fromJson(map));
//     }


//     List<PageModel> pageModelList = [
//       PageModel('第一页标题',true,page1ModelList),
//       PageModel('第二页标题',false,page2ModelList),
//       PageModel('第三页标题',true,page3ModelList),
//       PageModel('第四页标题',false,page4ModelList),
//       PageModel('第五页标题',true,page5ModelList),
//     ];

//     return Scaffold(
//       appBar: AppBar(
//         title: Text('会员分析'),
//       ),
//       body: PageView.builder(
//           controller: _pageController,
//           itemCount: pageModelList.length,
//           itemBuilder: (BuildContext context,int index){
//             return pageItem(pageModelList[index]);
//           },
//         ),
//     );
//   }

//   Widget pageItem(PageModel pageModel){
    
//     return Container(
//       child: Column(
//         children: <Widget>[
//           Row(
//             children: <Widget>[
//               Text(pageModel.isMultiSelect ? '多选题' : '单选题',style: TextStyle(backgroundColor: Colors.lightBlue,fontSize: 20),),
//               Text(pageModel.title,style: TextStyle(fontSize: 20),)
//             ],
//           ),
//           ListView.builder(
//             itemCount: pageModel.questionList.length,
//             shrinkWrap: true,
//             physics: NeverScrollableScrollPhysics(),
//             itemBuilder: (BuildContext context,int index){
//               QuestionModel questionModel = pageModel.questionList[index];
//               return InkWell(
//                 onTap: (){
//                   if (!pageModel.isMultiSelect) {
//                     for (QuestionModel item in pageModel.questionList) {
//                       item.isSelected = false;
//                     }
//                   }
//                   Provider.of<QuestionModel>(context).changeStatus(!questionModel.isSelected);
//                 },
//                 child: Container(
//                   child: Row(
//                     children: <Widget>[
//                       SizedBox(width: 10),
//                       Consumer<QuestionModel>(
//                         builder: (context,QuestionModel questionModel,child){
//                           print(questionModel.number);
//                           return Container(
//                             padding: EdgeInsets.all(3),
//                             decoration: BoxDecoration(
//                               shape: BoxShape.circle,
//                               border: Border.all(color: questionModel.isSelected ? Colors.red : Colors.transparent,width: 2),
//                             ),
//                             child: Center(
//                               child: Text(questionModel.number,style: TextStyle(fontSize: 20)),
//                             ),
//                           );
//                         }
//                       ),
                      
//                       Text(questionModel.questionTitle,style: TextStyle(fontSize: 20))
//                     ],
//                   )
//                 ),
//               );
//             },
//           )
//         ],
//       ),
//     );

//   }
// }


class PageModel extends Object{
  String title;
  bool isMultiSelect;
  List<QuestionModel> questionList;

  PageModel(this.title,this.isMultiSelect,this.questionList);
}

class QuestionModel with ChangeNotifier {
  String questionTitle = '';
  String number = '';
  bool isSelected = false;

  QuestionModel(this.questionTitle,this.number,this.isSelected);

  QuestionModel.fromJson(Map json){
    questionTitle = json['title'];
    number = json['No'];
    isSelected = false;
  }


  bool get status => isSelected;

  changeStatus(bool status){
    isSelected = status;
    notifyListeners();
  }

}